package xin.vote.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import xin.vote.model.Users;
import xin.vote.service.IUserService;
import xin.vote.util.Result;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private IUserService userService;
	
		//管理员登录
		@RequestMapping("/adminlogin")
		@ResponseBody
		public String adminLogin(@RequestParam(value = "account",required = false) String account,
								 @RequestParam(value = "password",required = false) String password) {
			Users u = new Users();
			u.setAccount(account);
			u.setPassword(password);
			Users admin = userService.adminLogin(u);
			if (admin == null) {
				return "0";
			} else {
				return "1";
			}
		}
	
	//用户登录
	@RequestMapping("/userlogin")
	@ResponseBody                     //对应前台页面的传过来的名称，如果不设置，则传过来的数据必须后者一致
	public Result UsersLogin(@RequestParam(value = "account") String account,
							 @RequestParam(value = "password") String password) {
		Users u = new Users();
		Result result = new Result();
		u.setAccount(account);
		u.setPassword(password);
		Users user = userService.findUserByNameAndPwd(u);
		if (user != null) {
			List<Users> users = new ArrayList<Users>();
			users.add(user);
			result.setCode(0);
			result.setMsg("1");
			result.setCount(1);
			result.setData(users);
		} else {
			result.setMsg("0");
		}
		return result;
	}

	//用户注册
	@RequestMapping("/adduser")
	@ResponseBody
	public String addUsers(@RequestParam(value = "account") String account,
			@RequestParam(value = "password") String password) {
		Users u = new Users();
		u.setAccount(account);
		u.setPassword(password);
		int ret = userService.addUserSelective(u);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	//查重用户名
	@RequestMapping(value = "/checkuser", method = RequestMethod.POST)
	@ResponseBody
	public String checkUsers(@RequestParam(value = "userLoginName") String userLoginName) {
		Users user = userService.findUserByAccount(userLoginName);
		if (user == null) {
			return "1";
		} else {
			return "0";
		}
	}
	
	//查询所有普通用户
	@RequestMapping("/getCommonUsers")
	@ResponseBody
	public Result findAllCommonUsers() {
		List<Users> userList = userService.findAllCommonUsers();
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(userList.size());
		result.setData(userList);
		return result;
	}
	
	//顶部动态查询
	@RequestMapping("/finduserdynamic")
	@ResponseBody
	public Result findUserDynamic(@RequestParam(value = "account") String account,
								  @RequestParam(value = "sex") String sex) {
		if(account.equals("")) {
			account = null;
		}
		if(sex.equals("")) {
			sex = null;
		}
		Users u = new Users(); 
		u.setAccount(account);
		u.setSex(sex);
		
		List<Users> userList = userService.findUsersDynamic(u);
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(userList.size());
		result.setData(userList);
		return result;
	}
	
	//编辑用户
	@RequestMapping("/updateuser")
	@ResponseBody
	public String updateUsers(
							  @RequestParam(value = "uid") Integer userid,
							  @RequestParam(value = "userloginname") String account,
							  @RequestParam(value = "username") String name,
							  @RequestParam(value = "usersex") String sex,
							  @RequestParam(value = "usertel") String telephone,
							  @RequestParam(value = "userscale") String scale){
		Users u = new Users();
		u.setUserid(userid);
		u.setAccount(account);
		u.setName(name);
		u.setSex(sex);
		u.setTelephone(telephone);
		u.setScale(scale);
		
		int ret = userService.updateByPrimaryKeySelective(u);
		if(ret>0) {
			return "1";
		}else {
			return "0";
		}
	}
	
	//删除用户
	@RequestMapping("/deleteuser")
	@ResponseBody
	public String deleteUsers(@RequestParam(value = "userid") Integer userid){	
		int ret = userService.deleteByPrimaryKey(userid);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}
	
	//管理员添加用户
	@RequestMapping("/adminadduser")
	@ResponseBody
	public String adminAddUser(@RequestParam(value = "userloginname") String account,
			 				   @RequestParam(value = "userpassword") String password,
			 				   @RequestParam(value = "username") String name,
			 				   @RequestParam(value = "usersex") String sex,
			 				   @RequestParam(value = "usertel") String telephone,
			 				   @RequestParam(value = "userscale") String scale) {
		
		Users u = new Users();
		u.setAccount(account);
		u.setPassword(password);
		u.setName(name);
		u.setSex(sex);
		u.setTelephone(telephone);
		u.setScale(scale);
		int ret = userService.insertUser(u);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}
}
